﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Oracle;
using System.Data.Common;


namespace GgCarControl
{
    public partial class STATUS2_ST : DockContent
    {
        public STATUS2_ST()
        {
            InitializeComponent();
        }
        public static Database db =
             new OracleDatabase("Data Source=DG;User ID=dgrfid;Password=dgrfid;Unicode=True;Load Balance Timeout=1000");
        //DataTable dt;

        //定义车辆登记信息表的delegate
        delegate void DataGridTruckLoginHandler(DataTable dt1, DataTable dt2, DataTable dt3, DataTable dt4);
        event DataGridTruckLoginHandler TruckLogin;

        private void button2_Click(object sender, EventArgs e)
        {
            load_data1();//已发进车指令的车辆
            load_data2();//已发卡的车辆
            load_data3();//已进门的车辆
            load_data4();//已理货的车辆
        }

        private DataTable load_data1()
        {
            //Begin time value
            string date_time_begin = this.dateTimePicker1.Value.ToString();
            int ind = date_time_begin.IndexOf(" ");
            string date_value_begin = date_time_begin.Substring(0, ind);
            //End time value
            string date_time_end = this.dateTimePicker2.Value.ToString();
            int ind2 = date_time_end.IndexOf(" ");
            string date_value_end = date_time_end.Substring(0, ind2);

            string sql1 = "select truckno as  车号,cargoname as 货名,SHIPNAME as 船名,BILL_NO as 提单号,logintime as 登记时间,yard as 地点,cardadd as 发卡地点,stackname as 垛名,pieces as 件数,IOYARDID,TRUCKSTATUS from TRUCK_CARGO_LOGIN" +
                " where ioyardid=0 and TRUCKSTATUS=1 and trunc(logintime)<=to_date('" + date_value_end + "','yyyy-mm-dd') and trunc(logintime)>=to_date('" + date_value_begin + "','yyyy-mm-dd') order by logintime desc,TRUCKSTATUS";
            DataTable dt1 = db.ExecuteDataSet(db.GetSqlStringCommand(sql1.ToString())).Tables[0];

            this.dataGridView1.AutoGenerateColumns = true;
            this.dataGridView1.AllowUserToAddRows = false;
            
            this.dataGridView1.DataSource = dt1;
            this.dataGridView1.Refresh();

            //设置某些字段不可见
            this.dataGridView1.Columns["IOYARDID"].Visible = false;
            this.dataGridView1.Columns["TRUCKSTATUS"].Visible = false;
            this.dataGridView1.Columns["登记时间"].Visible = false;
            this.dataGridView1.Columns["地点"].Visible = false;
            this.dataGridView1.Columns["发卡地点"].Visible = false;
            this.dataGridView1.Columns["垛名"].Visible = false;
            this.dataGridView1.Columns["件数"].Visible = false;
            //设置表格中不显示用来选择整个一行的那个列，并且设置为整行选择模式
            this.dataGridView1.RowHeadersVisible = false;
            this.dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            return dt1;
        }
        private DataTable load_data2()
        {
            //Begin time value
            string date_time_begin = this.dateTimePicker1.Value.ToString();
            int ind = date_time_begin.IndexOf(" ");
            string date_value_begin = date_time_begin.Substring(0, ind);
            //End time value
            string date_time_end = this.dateTimePicker2.Value.ToString();
            int ind2 = date_time_end.IndexOf(" ");
            string date_value_end = date_time_end.Substring(0, ind2);

            string sql = "select truckno as  车号,cargoname as 货名,SHIPNAME as 船名,BILL_NO as 提单号,logintime as 登记时间,yard as 地点,cardadd as 发卡地点,stackname as 垛名,pieces as 件数,IOYARDID,TRUCKSTATUS from TRUCK_CARGO_LOGIN" +
                " where ioyardid=0 and TRUCKSTATUS=2 and trunc(logintime)<=to_date('" + date_value_end + "','yyyy-mm-dd') and trunc(logintime)>=to_date('" + date_value_begin + "','yyyy-mm-dd') order by logintime desc,TRUCKSTATUS";
            DataTable dt = db.ExecuteDataSet(db.GetSqlStringCommand(sql.ToString())).Tables[0];

            this.dataGridView2.AutoGenerateColumns = true;
            this.dataGridView2.AllowUserToAddRows = false;
            this.dataGridView2.DataSource = dt;

            //设置某些字段不可见
            this.dataGridView2.Columns["IOYARDID"].Visible = false;
            this.dataGridView2.Columns["TRUCKSTATUS"].Visible = false;
            this.dataGridView2.Columns["登记时间"].Visible = false;
            this.dataGridView2.Columns["地点"].Visible = false;
            this.dataGridView2.Columns["发卡地点"].Visible = false;
            this.dataGridView2.Columns["垛名"].Visible = false;
            this.dataGridView2.Columns["件数"].Visible = false;
            //设置表格中不显示用来选择整个一行的那个列，并且设置为整行选择模式
            this.dataGridView2.RowHeadersVisible = false;
            this.dataGridView2.SelectionMode = DataGridViewSelectionMode.FullRowSelect;

            return dt;
        }
        private DataTable load_data3()
        {
            //Begin time value
            string date_time_begin = this.dateTimePicker1.Value.ToString();
            int ind = date_time_begin.IndexOf(" ");
            string date_value_begin = date_time_begin.Substring(0, ind);
            //End time value
            string date_time_end = this.dateTimePicker2.Value.ToString();
            int ind2 = date_time_end.IndexOf(" ");
            string date_value_end = date_time_end.Substring(0, ind2);

            string sql = "select truckno as  车号,cargoname as 货名,SHIPNAME as 船名,BILL_NO as 提单号,logintime as 登记时间,yard as 地点,cardadd as 发卡地点,stackname as 垛名,pieces as 件数,IOYARDID,TRUCKSTATUS from TRUCK_CARGO_LOGIN" +
                " where ioyardid=0 and TRUCKSTATUS=3 and trunc(logintime)<=to_date('" + date_value_end + "','yyyy-mm-dd') and trunc(logintime)>=to_date('" + date_value_begin + "','yyyy-mm-dd') order by logintime desc,TRUCKSTATUS";
            DataTable dt = db.ExecuteDataSet(db.GetSqlStringCommand(sql.ToString())).Tables[0];

            this.dataGridView3.AutoGenerateColumns = true;
            this.dataGridView3.AllowUserToAddRows = false;
            this.dataGridView3.DataSource = dt;

            //设置某些字段不可见
            this.dataGridView3.Columns["IOYARDID"].Visible = false;
            this.dataGridView3.Columns["TRUCKSTATUS"].Visible = false;
            this.dataGridView3.Columns["登记时间"].Visible = false;
            this.dataGridView3.Columns["地点"].Visible = false;
            this.dataGridView3.Columns["发卡地点"].Visible = false;
            this.dataGridView3.Columns["垛名"].Visible = false;
            this.dataGridView3.Columns["件数"].Visible = false;
            //设置表格中不显示用来选择整个一行的那个列，并且设置为整行选择模式
            this.dataGridView3.RowHeadersVisible = false;
            this.dataGridView3.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            return dt;
        }
        private DataTable load_data4()
        {
            //Begin time value
            string date_time_begin = this.dateTimePicker1.Value.ToString();
            int ind = date_time_begin.IndexOf(" ");
            string date_value_begin = date_time_begin.Substring(0, ind);
            //End time value
            string date_time_end = this.dateTimePicker2.Value.ToString();
            int ind2 = date_time_end.IndexOf(" ");
            string date_value_end = date_time_end.Substring(0, ind2);

            string sql = "select truckno as  车号,cargoname as 货名,SHIPNAME as 船名,BILL_NO as 提单号,logintime as 登记时间,yard as 地点,cardadd as 发卡地点,stackname as 垛名,pieces as 件数,IOYARDID,TRUCKSTATUS from TRUCK_CARGO_LOGIN" +
                " where ioyardid=0 and TRUCKSTATUS=4 and trunc(logintime)<=to_date('" + date_value_end + "','yyyy-mm-dd') and trunc(logintime)>=to_date('" + date_value_begin + "','yyyy-mm-dd') order by logintime desc,TRUCKSTATUS";
            DataTable dt = db.ExecuteDataSet(db.GetSqlStringCommand(sql.ToString())).Tables[0];

            this.dataGridView4.AutoGenerateColumns = true;
            this.dataGridView4.AllowUserToAddRows = false;
            this.dataGridView4.DataSource = dt;

            //设置某些字段不可见
            this.dataGridView4.Columns["IOYARDID"].Visible = false;
            this.dataGridView4.Columns["TRUCKSTATUS"].Visible = false;
            this.dataGridView4.Columns["登记时间"].Visible = false;
            this.dataGridView4.Columns["地点"].Visible = false;
            this.dataGridView4.Columns["发卡地点"].Visible = false;
            this.dataGridView4.Columns["垛名"].Visible = false;
            this.dataGridView4.Columns["件数"].Visible = false;
            //设置表格中不显示用来选择整个一行的那个列，并且设置为整行选择模式
            this.dataGridView4.RowHeadersVisible = false;
            this.dataGridView4.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            return dt;
        }

        void Add_DataTable(DataTable dt1, DataTable dt2, DataTable dt3, DataTable dt4)
        {
            
            //this.dataGridView1.DataSource = dt1;
            //this.dataGridView2.DataSource = dt2;
            //this.dataGridView3.DataSource = dt3;
            //this.dataGridView4.DataSource = dt4;

            this.dataGridView1.AutoGenerateColumns = true;
            this.dataGridView1.AllowUserToAddRows = false;
            this.dataGridView1.DataSource = dt1;
            //设置某些字段不可见
            this.dataGridView1.Columns["IOYARDID"].Visible = false;
            this.dataGridView1.Columns["TRUCKSTATUS"].Visible = false;
            this.dataGridView1.Columns["登记时间"].Visible = false;
            this.dataGridView1.Columns["地点"].Visible = false;
            this.dataGridView1.Columns["发卡地点"].Visible = false;
            this.dataGridView1.Columns["垛名"].Visible = false;
            this.dataGridView1.Columns["件数"].Visible = false;
            //设置表格中不显示用来选择整个一行的那个列，并且设置为整行选择模式
            this.dataGridView1.RowHeadersVisible = false;
            this.dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;

            this.dataGridView2.AutoGenerateColumns = true;
            this.dataGridView2.AllowUserToAddRows = false;
            this.dataGridView2.DataSource = dt2;
            //设置某些字段不可见
            this.dataGridView2.Columns["IOYARDID"].Visible = false;
            this.dataGridView2.Columns["TRUCKSTATUS"].Visible = false;
            this.dataGridView2.Columns["登记时间"].Visible = false;
            this.dataGridView2.Columns["地点"].Visible = false;
            this.dataGridView2.Columns["发卡地点"].Visible = false;
            this.dataGridView2.Columns["垛名"].Visible = false;
            this.dataGridView2.Columns["件数"].Visible = false;
            //设置表格中不显示用来选择整个一行的那个列，并且设置为整行选择模式
            this.dataGridView2.RowHeadersVisible = false;
            this.dataGridView2.SelectionMode = DataGridViewSelectionMode.FullRowSelect;

            this.dataGridView3.AutoGenerateColumns = true;
            this.dataGridView3.AllowUserToAddRows = false;
            this.dataGridView3.DataSource = dt3;
            //设置某些字段不可见
            this.dataGridView3.Columns["IOYARDID"].Visible = false;
            this.dataGridView3.Columns["TRUCKSTATUS"].Visible = false;
            this.dataGridView3.Columns["登记时间"].Visible = false;
            this.dataGridView3.Columns["地点"].Visible = false;
            this.dataGridView3.Columns["发卡地点"].Visible = false;
            this.dataGridView3.Columns["垛名"].Visible = false;
            this.dataGridView3.Columns["件数"].Visible = false;
            //设置表格中不显示用来选择整个一行的那个列，并且设置为整行选择模式
            this.dataGridView3.RowHeadersVisible = false;
            this.dataGridView3.SelectionMode = DataGridViewSelectionMode.FullRowSelect;

            this.dataGridView4.AutoGenerateColumns = true;
            this.dataGridView4.AllowUserToAddRows = false;
            this.dataGridView4.DataSource = dt4;
            //设置某些字段不可见
            this.dataGridView4.Columns["IOYARDID"].Visible = false;
            this.dataGridView4.Columns["TRUCKSTATUS"].Visible = false;
            this.dataGridView4.Columns["登记时间"].Visible = false;
            this.dataGridView4.Columns["地点"].Visible = false;
            this.dataGridView4.Columns["发卡地点"].Visible = false;
            this.dataGridView4.Columns["垛名"].Visible = false;
            this.dataGridView4.Columns["件数"].Visible = false;
            //设置表格中不显示用来选择整个一行的那个列，并且设置为整行选择模式
            this.dataGridView4.RowHeadersVisible = false;
            this.dataGridView4.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
        }

        System.Timers.Timer t = new System.Timers.Timer(30000);//实例化Timer类，设置间隔时间为10000毫秒；

        private void STATUS2_ST_Load(object sender, EventArgs e)
        {
            TruckLogin += new DataGridTruckLoginHandler(Add_DataTable);
            
            //System.Timers.Timer t = new System.Timers.Timer(6000);//实例化Timer类，设置间隔时间为10000毫秒；
            t.Elapsed += new System.Timers.ElapsedEventHandler(theout);//到达时间的时候执行事件；
            t.AutoReset = true;//设置是执行一次（false）还是一直执行(true)；
            t.Enabled = true;//是否执行System.Timers.Timer.Elapsed事件；
        }
        
        
        public void theout(object source, System.Timers.ElapsedEventArgs e)
        {
            //Begin time value
            string date_time_begin = this.dateTimePicker1.Value.ToString();
            int ind = date_time_begin.IndexOf(" ");
            string date_value_begin = date_time_begin.Substring(0, ind);
            //End time value
            string date_time_end = this.dateTimePicker2.Value.ToString();
            int ind2 = date_time_end.IndexOf(" ");
            string date_value_end = date_time_end.Substring(0, ind2);

            string sql1 = "select truckno as  车号,cargoname as 货名,SHIPNAME as 船名,BILL_NO as 提单号,logintime as 登记时间,yard as 地点,cardadd as 发卡地点,stackname as 垛名,pieces as 件数,IOYARDID,TRUCKSTATUS from TRUCK_CARGO_LOGIN" +
               " where ioyardid=0 and TRUCKSTATUS=1 and trunc(logintime)<=to_date('" + date_value_end + "','yyyy-mm-dd') and trunc(logintime)>=to_date('" + date_value_begin + "','yyyy-mm-dd') order by logintime desc,TRUCKSTATUS";
            DataTable dt1 = db.ExecuteDataSet(db.GetSqlStringCommand(sql1.ToString())).Tables[0];

            string sql2 = "select truckno as  车号,cargoname as 货名,SHIPNAME as 船名,BILL_NO as 提单号,logintime as 登记时间,yard as 地点,cardadd as 发卡地点,stackname as 垛名,pieces as 件数,IOYARDID,TRUCKSTATUS from TRUCK_CARGO_LOGIN" +
               " where ioyardid=0 and TRUCKSTATUS=2 and trunc(logintime)<=to_date('" + date_value_end + "','yyyy-mm-dd') and trunc(logintime)>=to_date('" + date_value_begin + "','yyyy-mm-dd') order by logintime desc,TRUCKSTATUS";
            DataTable dt2 = db.ExecuteDataSet(db.GetSqlStringCommand(sql2.ToString())).Tables[0];

            string sql3 = "select truckno as  车号,cargoname as 货名,SHIPNAME as 船名,BILL_NO as 提单号,logintime as 登记时间,yard as 地点,cardadd as 发卡地点,stackname as 垛名,pieces as 件数,IOYARDID,TRUCKSTATUS from TRUCK_CARGO_LOGIN" +
               " where ioyardid=0 and TRUCKSTATUS=3 and trunc(logintime)<=to_date('" + date_value_end + "','yyyy-mm-dd') and trunc(logintime)>=to_date('" + date_value_begin + "','yyyy-mm-dd') order by logintime desc,TRUCKSTATUS";
            DataTable dt3 = db.ExecuteDataSet(db.GetSqlStringCommand(sql3.ToString())).Tables[0];

            string sql4 = "select truckno as  车号,cargoname as 货名,SHIPNAME as 船名,BILL_NO as 提单号,logintime as 登记时间,yard as 地点,cardadd as 发卡地点,stackname as 垛名,pieces as 件数,IOYARDID,TRUCKSTATUS from TRUCK_CARGO_LOGIN" +
               " where ioyardid=0 and TRUCKSTATUS=4 and trunc(logintime)<=to_date('" + date_value_end + "','yyyy-mm-dd') and trunc(logintime)>=to_date('" + date_value_begin + "','yyyy-mm-dd') order by logintime desc,TRUCKSTATUS";
            DataTable dt4 = db.ExecuteDataSet(db.GetSqlStringCommand(sql4.ToString())).Tables[0];
            
            this.dataGridView1.BeginInvoke(TruckLogin, dt1, dt2, dt3, dt4);
        }
        
    }
}
